const logger = require('../logger');
const db = require('../../db');

class GrabSource {
    /*
    constructor() {

    }
    */

    /**
     * 抓取站点配置列表
     */
    getGrabSourceList(req) {
        let station_id = '';
        let condition = {};
        if (req.params) {
            station_id = req.params.station_id;
            if (station_id) {
                condition = { station_id };
            }
        }
        logger.data.info('model.Grab.getGrabSourceList req：', req.params);
        return new Promise(function (resolve, reject) {
            // let condition = {};
            db.grab_source_conf.findAndCountAll({
                // attributes: [],
                where: condition,
                order: [
                    ['update_time', 'desc']
                ],
                include: [
                    {
                        model: db.data_source,
                        as: 'dataSource'
                    },
                    {
                        model: db.category,
                        as: 'category'
                    },
                    {
                        model: db.info_type,
                        as: 'infoType'
                    },
                ]
            }).then(function (result) {
                resolve(result);
            }, function (err) {
                logger.error.error('model.grabSource.getGrabSourceList error：', err);
                reject(err);
            });
        });
    }

    /**
     * 保存站点配置
     */
    saveGrabSource(req) {
        let reqData = req.body;
        let { station_id } = reqData;
        const now = new Date();
        const uid = req.userInfo.uid;
        logger.data.info('model.grabSource.saveGrabSource req：', reqData);
        return new Promise(function (resolve, reject) {
            reqData.updator = uid;
            reqData.update_time = now;
            // 下面这些字段不必填，给默认值
            reqData.is_download_attachment = 1;
            reqData.is_download_vedio = 0;
            reqData.is_handle_content = 0;
            reqData.is_handle_pic = 0;
            reqData.is_handle_table = 0;
            reqData.is_semantic_analysis = 0;
            reqData.is_need_edit = 0;
            reqData.is_corpus = 0;
            reqData.is_text_mining = 0;
            reqData.is_force_edit = 0;
            reqData.crawlerInfo = 0;

            if (Number(station_id)) {
                logger.data.info('model.grabSource.saveGrabSource update：', reqData);
                db.grab_source_conf.update(reqData, {
                    where: { station_id }
                }).then(result => {
                    resolve(result[0]);
                }).catch(e => {
                    logger.error.error('model.grabSource.saveGrabSource update error：', e);
                    resolve('');
                });
            } else {
                reqData.creator = uid;
                reqData.creat_time = now;
                logger.data.info('model.grabSource.saveGrabSource create：', reqData);
                db.grab_source_conf.create(reqData).then(result => {
                    resolve(result);
                }).catch(e => {
                    logger.error.error('model.grabSource.saveGrabSource create error：', e);
                    resolve('');
                });
            }
        });
    }
}


module.exports = new GrabSource();
